
package com.the9.C9Alarm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;


public class boxAlarmActivity extends Activity implements RefreshAlarm {
    private final static String TAG                     = "alarm";

    RelativeLayout m_layoutMain;

   // ListView m_alarmList;

    Media mMedia;
    Button m_btStopSleepAlarm;
    RelativeLayout tl,tr,br,bl;
    TextView tvtlname,tvtltime,tvtlring;
    TextView tvblname,tvbltime,tvblring;
    TextView tvtrname,tvtrtime,tvtrring;
    TextView tvbrname,tvbrtime,tvbrring;
    ImageButton blm,tlm,trm,brm;
    ImageView imageView_cirtl,imageView_cirtr,imageView_cirbl,imageView_cirbr;
    CenterCirView cv;
    Form mForm[];
    private List<AlarmBean> m_listBean;
    

    
//    Button m_btChangeBg;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long a = System.currentTimeMillis();
        long b;
        //set no title mode
       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mainm);
       // HolidayReceiver.setAlarm(this);
        
        
        
        
        mMedia= Media.getInstance(this);
        b=System.currentTimeMillis();
        Log.e("initfirst Use time", ""+(b-a));
        a=b;
        
        initUI();
        b=System.currentTimeMillis();
        Log.e("initUI Use time", ""+(b-a));
        a=b;
        setListener();
        b=System.currentTimeMillis();
        Log.e("setListener Use time", ""+(b-a));
        a=b;
        setUI();
        b=System.currentTimeMillis();
        Log.e("setUI Use time", ""+(b-a));
        a=b;
        setForm();
        b=System.currentTimeMillis();
        Log.e("setForm Use time", ""+(b-a));
        a=b;
    }

    @Override
    protected void onResume() {
        refreshUI();
        super.onResume();
    }
    private void initUI(){
    	 // m_layoutMain = (RelativeLayout) findViewById(R.id.layoutMain);
    	  m_btStopSleepAlarm= (Button) findViewById(R.id.btStop);
    	  
    	//  tl =(RelativeLayout) findViewById(R.id.include1);
//    	  tvtlname = (TextView) findViewById(R.id.texttlname);
//    	  tvtlring = (TextView) findViewById(R.id.texttltime);
//          tvtltime = (TextView) findViewById(R.id.texttlring);
          tlm = (ImageButton) findViewById(R.id.imageBttl);
          imageView_cirtl = (ImageView) findViewById(R.id.imageView_cirtl);
          
       //   bl =(RelativeLayout) findViewById(R.id.include3);
//    	  tvblname = (TextView) findViewById(R.id.textblname);
//          tvbltime = (TextView) findViewById(R.id.textbltime);
//          tvblring = (TextView) findViewById(R.id.textblring);
          blm = (ImageButton) findViewById(R.id.imageBtbl);
          imageView_cirtr = (ImageView) findViewById(R.id.imageView_cirtr);
          
       //   tr =(RelativeLayout) findViewById(R.id.include2);
//    	  tvtrname = (TextView) findViewById(R.id.texttrname);
//          tvtrtime = (TextView) findViewById(R.id.texttrtime);
//          tvtrring = (TextView) findViewById(R.id.texttrring);
          trm = (ImageButton) findViewById(R.id.imageBttr);
          imageView_cirbl = (ImageView) findViewById(R.id.imageView_cirbl);
          
       //   br =(RelativeLayout) findViewById(R.id.include4);
//    	  tvbrname = (TextView) findViewById(R.id.textbrname);
//          tvbrtime = (TextView) findViewById(R.id.textbrtime);
//          tvbrring = (TextView) findViewById(R.id.textbrring);
          brm = (ImageButton) findViewById(R.id.imageBtbr);
          imageView_cirbr = (ImageView) findViewById(R.id.imageView_cirbr);
          
          cv = (CenterCirView) findViewById(R.id.analogClock1);
          
    }

	private void setListener() {
		m_btStopSleepAlarm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Utils.forceStopSleepAlarm(boxAlarmActivity.this);
			}

		});
		// m_alarmList = (ListView) findViewById(R.id.alarmList);

		tlm.setOnClickListener(new KeyListener(0));
		imageView_cirtl.setOnClickListener(new StateKeyListener(0));
		trm.setOnClickListener(new KeyListener(1));
		imageView_cirtr.setOnClickListener(new StateKeyListener(1));
		blm.setOnClickListener(new KeyListener(2));
		imageView_cirbl.setOnClickListener(new StateKeyListener(2));
		brm.setOnClickListener(new KeyListener(3));
		imageView_cirbr.setOnClickListener(new StateKeyListener(3));

	}
	
	private void setForm(){
		mForm = new Form[4];
		mForm[0] = Form.getForm(m_listBean.get(0).getForm());
		mForm[1] = Form.getForm(m_listBean.get(1).getForm());
		mForm[2] = Form.getForm(m_listBean.get(2).getForm());
		mForm[3]= Form.getForm(m_listBean.get(3).getForm());
		if(mForm[0]!=null){
			tlm.setImageResource(mForm[0].getMainPic());
			//Log.e("aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaa");
		}
	}

    private void setUI() {
    	m_listBean=AlarmProvider.geInstance(this).getAlarms();       
    	try{
    		cv.onAlarmChange(AlarmProvider.geInstance(this).getNextAlarm().getIndex(),AlarmProvider.geInstance(this).getNextAlarm().getTime());
    	}catch(Exception e){
    		  cv.onAlarmChange(-1,-1l);
    	}
       
        
//        tvtlname.setText(m_listBean.get(0).getName());     
//        tvtltime.setText(Utils.getCalendarTime(m_listBean.get(0).getTime()));     
//        tvtlring.setText( Utils.formatRingMode(m_listBean.get(0).getRingMode()));
        
       
           
            
//            tvtrname.setText(m_listBean.get(1).getName());     
//            tvtrtime.setText(Utils.getCalendarTime(m_listBean.get(1).getTime()));     
//            tvtrring.setText( Utils.formatRingMode(m_listBean.get(1).getRingMode()));
//            
//            
//            
//            tvblname.setText(m_listBean.get(2).getName());     
//            tvbltime.setText(Utils.getCalendarTime(m_listBean.get(2).getTime()));     
//            tvblring.setText( Utils.formatRingMode(m_listBean.get(2).getRingMode()));
//            
//          
//            
//            tvbrname.setText(m_listBean.get(3).getName());     
//            tvbrtime.setText(Utils.getCalendarTime(m_listBean.get(3).getTime()));     
//            tvbrring.setText( Utils.formatRingMode(m_listBean.get(3).getRingMode()));
            
            
            
            setButtonState();
            
            if(AlarmProvider.geInstance(this).isSleepAlarmEnable(0)){
            	m_btStopSleepAlarm.setEnabled(true);
            }else{
            	m_btStopSleepAlarm.setEnabled(false);
            }
//      
    }

    private void setButtonState(){
    	for(int i=0;i<4;i++){
    		setButtonState(i,m_listBean.get(i).getEnabled());
    	}
    }
    private void setButtonState(int i,boolean state){
    	ImageView iv = null;
    	switch(i){
    	case 0:{
    		iv= imageView_cirtl;
    		break;
    	}
    	case 1:{
    		iv = imageView_cirtr;
    		break;
    		
    	}
    	case 2:{
    		iv= imageView_cirbl;
    		break;
    	}
    	case 3:{
    		iv=imageView_cirbr;
    		break;
    	}
    	default : return;
    	
    	}
    	int alpha  = 255;
    	if(state==false){
    		alpha = 0;
    	}
    	iv.setAlpha(alpha);
    	
    	//iv.setAlpha(alpha)
   // 	iv.setVisibility(View.INVISIBLE);
    	iv.invalidate();
    }
   

    

    @Override
    public void refreshData() {
        AlarmProvider ap = AlarmProvider.geInstance(this.getApplicationContext());
        for (int i = 0; i <m_listBean.size(); i++) {
            ap.setAlarmEnable(i, (m_listBean.get(i).isEnabled()));
        }

       
            AlarmSet.setAlarm(this);
       
    }
    public void refreshData(int i) {
    	  AlarmProvider ap = AlarmProvider.geInstance(this.getApplicationContext());
          ap.setAlarmEnable(i, (m_listBean.get(i).isEnabled()));
          AlarmSet.setAlarm(this);
          AlarmBean a= AlarmProvider.geInstance(this).getNextAlarm();
          if(a!=null)
          cv.onAlarmChange(a.getIndex(),a.getTime());
          else{
        	  cv.onAlarmChange(-1,-1l);
          }
          
    }

     @Override
    public void refreshUI() {
       
    	 setUI();
    }
    
	/**
	 * 选择闹铃按钮
	 * */
	class KeyListener implements OnClickListener {

		private int id;

		public KeyListener(int id) {
			super();
			// TODO Auto-generated constructor stub
			this.id = id;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//ShareDActivity.Share(boxAlarmActivity.this,null,null);
			
			if(mForm[id]!=null){
				mForm[id].playClickSound();
			}
			Bundle bundle = new Bundle();
			bundle.putInt("alarm_position", id);
			Intent intent = new Intent(boxAlarmActivity.this,
					SetAlarmActivity.class);
			intent.putExtras(bundle);
			boxAlarmActivity.this.startActivity(intent);
		}
	};

	/**
	 * 是否启用的状态按钮
	 * */
	class StateKeyListener implements OnClickListener {

		private int id;

		public StateKeyListener(int id) {
			super();
			// TODO Auto-generated constructor stub
			this.id = id;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			boolean state = !m_listBean.get(id).getEnabled();
			m_listBean.get(id).setEnabled(state);
			setButtonState(id, state);
			refreshData(id);

		}
	};
}
